﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data.Entity;
using System.Data.Entity.ModelConfiguration.Conventions;
using System.Data.Entity.Infrastructure;

namespace PGE.AfastamentosTempMVC.Modelo
{
    public class Contexto : DbContext
    {

        public Contexto()
            : base("Afastamento")
        { }

        public DbSet<Afastamento> Afastamentos { get; set; }
        public DbSet<Funcionario> Funcionarios { get; set; }
        public DbSet<Nucleo> Nucleos { get; set; }
        public DbSet<Setor> Setores { get; set; }
        public DbSet<TipoAfastamento> TipoAfastamentos { get; set; }

        protected override void OnModelCreating(DbModelBuilder modelBuilder)
        {
            modelBuilder.Conventions.Remove<IncludeMetadataConvention>();
            modelBuilder.Configurations.Add(new TipoAfastamento.Mapping());
            modelBuilder.Entity<Nucleo>().HasRequired(r => r.Setor).WithMany(a => a.Nucleos).WillCascadeOnDelete(false);

            modelBuilder.Entity<Funcionario>().HasRequired(a => a.Setor).WithMany().HasForeignKey(u => u.IdSetor);

            modelBuilder.Entity<Funcionario>().HasRequired(a => a.Setor).WithMany().HasForeignKey(u => u.IdSetor).WillCascadeOnDelete(false);

            modelBuilder.Entity<Funcionario>().HasRequired(a => a.Nucleo).WithMany().HasForeignKey(u => u.IdNucleo);

            modelBuilder.Entity<Funcionario>().HasRequired(a => a.Nucleo).WithMany().HasForeignKey(u => u.IdNucleo).WillCascadeOnDelete(false);




        }

    }
}